package com.eastfair.venueservice.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.StrUtil;
//import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.base.entity.SuperEntity;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.venueservice.consts.CommonConstants;
import com.eastfair.venueservice.dao.ExhibitionSpaceDrawingMapper;
import com.eastfair.venueservice.dto.ExhibitionSpaceDrawingDTO;
import com.eastfair.venueservice.dto.VenueDrawingAuditDTO;
import com.eastfair.venueservice.entity.*;
import com.eastfair.venueservice.service.ExhibitionSpaceDrawingService;
import com.eastfair.venueservice.service.HomeBuilderDrawingAuditService;
import com.eastfair.venueservice.service.VenueDrawingAuditService;
import com.eastfair.venueservice.vo.ExhibitionSpaceDrawingVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务实现类
 * 展会展厅图纸
 * </p>
 *
 * @author dqq
 * @date 2022-11-03
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class ExhibitionSpaceDrawingServiceImpl extends SuperServiceImpl<ExhibitionSpaceDrawingMapper, ExhibitionSpaceDrawing> implements ExhibitionSpaceDrawingService {

    @Resource
    private VenueDrawingAuditService venueDrawingAuditService;

    @Resource
    private HomeBuilderDrawingAuditService homeBuilderDrawingAuditService;

    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<ExhibitionSpaceDrawing> modelList) {
        modelList.forEach(s -> s.setIsDeleted(BusinessConstant.YES));
        return R.successDef();
    }

    @Override
    protected R<Boolean> handlerSave(ExhibitionSpaceDrawing model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        return R.successDef();
    }

    @Override
    public List<ExhibitionSpaceDrawingVO> listExhibitionSpaceDrawingVO(ExhibitionSpaceDrawingDTO query) {
        log.info("listExhibitionSpaceDrawingVO - 查询展会展厅图纸VO, query={}", query);
        List<ExhibitionSpaceDrawing> list = listExhibitionSpaceDrawing(query);
        if (list == null || list.isEmpty()) {
            return Collections.emptyList();
        }
        List<ExhibitionSpaceDrawingVO> voList = list
                .stream()
                .map(exhibitionSpaceDrawing -> BeanUtil.toBean(exhibitionSpaceDrawing, ExhibitionSpaceDrawingVO.class))
                .collect(Collectors.toList());
        return voList;
    }

    @Override
    public List<ExhibitionSpaceDrawing> listExhibitionSpaceDrawing(ExhibitionSpaceDrawingDTO query) {
        QueryWrapper<ExhibitionSpaceDrawing> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .lambda()
                .eq(ExhibitionSpaceDrawing::getIsDeleted, BusinessConstant.DELETE_NO)
                .eq(query.getExhibitionManageId() != null, ExhibitionSpaceDrawing::getExhibitionManageId, query.getExhibitionManageId())
                .eq(StrUtil.isNotBlank(query.getSpaceCode()), ExhibitionSpaceDrawing::getSpaceCode, query.getSpaceCode())
                .eq(StrUtil.isNotBlank(query.getSiteCode()), ExhibitionSpaceDrawing::getSiteCode, query.getSiteCode())
                .eq(StrUtil.isNotBlank(query.getSiteName()), ExhibitionSpaceDrawing::getSiteName, query.getSiteName())
                .eq(query.getDrawingId() != null, ExhibitionSpaceDrawing::getDrawingId, query.getDrawingId())
                .in(query.getImageUrlList() != null && !query.getImageUrlList().isEmpty(), ExhibitionSpaceDrawing::getImageUrl, query.getImageUrlList())
        ;
        return list(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ExhibitionSpaceDrawing saveExhibitionSpaceDrawing(ExhibitionSpaceDrawingDTO exhibitionSpaceDrawingDTO) {
        log.info("saveExhibitionSpaceDrawing - 保存展会展厅图纸, exhibitionSpaceDrawingDTO={}", exhibitionSpaceDrawingDTO);
        ExhibitionSpaceDrawing exhibitionSpaceDrawing = BeanUtil.toBean(exhibitionSpaceDrawingDTO, ExhibitionSpaceDrawing.class);
        save(exhibitionSpaceDrawing);
        return exhibitionSpaceDrawing;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatch(List<ExhibitionSpaceDrawingDTO> list) {
        log.info("saveBatch - 批量保存展会展厅图纸, list={}", list);
        if (list == null || list.isEmpty()) {
            return true;
        }
        list.forEach(this::saveExhibitionSpaceDrawing);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatch(List<ExhibitionSpaceDrawingDTO> list, Long exhibitionManageId, String spaceCode, Long drawingId) {
        removeByExhibitionManageId(exhibitionManageId, spaceCode, drawingId);
        return saveBatch(list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatchByVenueDrawing(VenueDrawingCategoryAudit venueDrawingCategoryAudit) {
        // 查询该展会展厅下图纸分类的审核图纸列表
        VenueDrawingAuditDTO venueDrawingAuditDTO = new VenueDrawingAuditDTO();
        venueDrawingAuditDTO.setExhibitionManageId(venueDrawingCategoryAudit.getExhibitionManageId());
        venueDrawingAuditDTO.setVenueReportId(venueDrawingCategoryAudit.getVenueReportId());
        venueDrawingAuditDTO.setSpaceCode(venueDrawingCategoryAudit.getSpaceCode());
        venueDrawingAuditDTO.setDrawingId(venueDrawingCategoryAudit.getDrawingId());
        List<VenueDrawingAudit> venueDrawingAuditList = venueDrawingAuditService.listVenueDrawingAudit(venueDrawingAuditDTO);
        if (venueDrawingAuditList == null || venueDrawingAuditList.isEmpty()) {
            return true;
        }
        // 批量保存展会展厅图纸
        CopyOptions copyOptions = CopyOptions
                .create()
                .setIgnoreProperties(CommonConstants.COMMON_IGNORE_PROPERTIES);
        List<ExhibitionSpaceDrawingDTO> exhibitionSpaceDrawingList = venueDrawingAuditList
                .stream()
                .map(venueDrawingAudit -> BeanUtil.toBean(venueDrawingAudit, ExhibitionSpaceDrawingDTO.class, copyOptions))
                .collect(Collectors.toList());
        return saveBatch(exhibitionSpaceDrawingList, venueDrawingCategoryAudit.getExhibitionManageId(),
                venueDrawingCategoryAudit.getSpaceCode(), venueDrawingCategoryAudit.getDrawingId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatchByHomeBuilderDrawingCategoryAudit(List<HomeBuilderDrawingCategoryAudit> list) {
        if (list == null || list.isEmpty()) {
            return false;
        }
        // 查询图纸列表
        List<Long> categoryAuditIds = list
                .stream()
                .map(SuperEntity::getId)
                .collect(Collectors.toList());
        List<HomeBuilderDrawingAudit> homeBuilderDrawingAuditList = homeBuilderDrawingAuditService.listByCategoryAuditIds(categoryAuditIds);
        if (homeBuilderDrawingAuditList == null || homeBuilderDrawingAuditList.isEmpty()) {
            return false;
        }
        // 删除图纸
        List<String> imageUrls = homeBuilderDrawingAuditList
                .stream()
                .map(HomeBuilderDrawingAudit::getImageUrl)
                .collect(Collectors.toList());
        removeByImageUrl(imageUrls);
        // 批量保存图纸
        List<ExhibitionSpaceDrawingDTO> exhibitionSpaceDrawingList = homeBuilderDrawingAuditList
                .stream()
                .map(this::toExhibitionSpaceDrawingDTO)
                .collect(Collectors.toList());
        return saveBatch(exhibitionSpaceDrawingList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByExhibitionManageId(Long exhibitionManageId, String spaceCode, Long drawingId) {
        log.info("removeByExhibitionManageId - 移除展会的展厅的指定图纸类型的图纸, exhibitionManageId={}, spaceCode={}, drawingId={}",
                exhibitionManageId, spaceCode, drawingId);
        ExhibitionSpaceDrawingDTO query = new ExhibitionSpaceDrawingDTO();
        query.setExhibitionManageId(exhibitionManageId);
        query.setSpaceCode(spaceCode);
        query.setDrawingId(drawingId);
        List<ExhibitionSpaceDrawing> list = listExhibitionSpaceDrawing(query);
        if (list == null || list.isEmpty()) {
            return true;
        }
        return removeByIdsOfLogic(list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByImageUrl(List<String> imageUrls) {
        ExhibitionSpaceDrawingDTO query = new ExhibitionSpaceDrawingDTO();
        query.setImageUrlList(imageUrls);
        List<ExhibitionSpaceDrawing> list = listExhibitionSpaceDrawing(query);
        if (list == null || list.isEmpty()) {
            return false;
        }
        return removeByIdsOfLogic(list);
    }

    private ExhibitionSpaceDrawingDTO toExhibitionSpaceDrawingDTO(HomeBuilderDrawingAudit homeBuilderDrawingAudit) {
        ExhibitionSpaceDrawingDTO exhibitionSpaceDrawingDTO = new ExhibitionSpaceDrawingDTO();
        exhibitionSpaceDrawingDTO.setExhibitionManageId(homeBuilderDrawingAudit.getExhibitionManageId());
        exhibitionSpaceDrawingDTO.setExhibitionManageName(homeBuilderDrawingAudit.getExhibitionManageName());
        exhibitionSpaceDrawingDTO.setSpaceCode(homeBuilderDrawingAudit.getSpaceCode());
        exhibitionSpaceDrawingDTO.setSpaceName(homeBuilderDrawingAudit.getSpaceName());
        exhibitionSpaceDrawingDTO.setSiteCode(homeBuilderDrawingAudit.getSiteCode());
        exhibitionSpaceDrawingDTO.setSiteName(homeBuilderDrawingAudit.getSiteName());
        exhibitionSpaceDrawingDTO.setDrawingId(homeBuilderDrawingAudit.getDrawingId());
        exhibitionSpaceDrawingDTO.setDrawingName(homeBuilderDrawingAudit.getDrawingName());
        exhibitionSpaceDrawingDTO.setImageUrl(homeBuilderDrawingAudit.getImageUrl());
        return exhibitionSpaceDrawingDTO;
    }
}
